﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace CGF
{
    [AddComponentMenu("CGFramework/SwitchGroup")]
    public class SwitchGroup : MonoBehaviour
    {
        public int defaultSelected;

        public RectTransform rectTransform { get { return transform as RectTransform; } }
        private int _currIndex;

        public int CurrIndex
        {
            get { return _currIndex; }
            set
            {
                _OnSwitchOn(value);
            }
        }

        public Switch[] switchs;

        public Action<int> onSwitchChange;

        private void Awake()
        {
            _currIndex = defaultSelected;
            for (int i = 0; i < switchs.Length; i++)
            {
                switchs[i].InitByGroup(this, i);
                switchs[i].SetIsOn(i == _currIndex, false);
            }
        }

        public void _OnSwitchOn(int index)
        {
            this._currIndex = index;
            onSwitchChange?.Invoke(index);
            for (int i = 0; i < switchs.Length; i++)
            {
                Switch button = switchs[i];
                if (button)
                {
                    button.SetIsOn(i == index, false);
                }
            }
        }
    }
}
